<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

define('STOCK_DECIMAL_PLACES',2);

class stockmarket extends template_plugin
{
	var $ipsclass;
	var $cache_array = array(
								'ibm_stock_company' => array('Company cache','company_cache'),
							 );
	
	var $name = "Stock Market";
	var $multiple = 0;
	
	function company_cache()
	{
		$this->ipsclass->cache['ibm_stock_company'] = array();
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_stock_companies'));
		$this->ipsclass->DB->exec_query();
		while($company = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->cache['ibm_stock_company'][$company['c_id']] = $company;
		}
		
		$this->ipsclass->update_cache( array( 'name' => 'ibm_stock_company', 'array' => 1, 'deletefirst' => 1 ) );
	}
	
	function read_quote_yahoo_fiannce($symbol = '')
	{
		if($symbol == "")
		{
			return false;
		}
		
		$fh = fopen("http://download.finance.yahoo.com/d/quotes.csv?s=".urlencode($symbol)."&f=sl1d1t1c1ohgv&e=.csv","r");
		$contents = fgetcsv($fh);
		if(count($contents) < 2 || $contents[1] == 0)
		{
			return false;
		}
		
		$quote = array();
		$quote['symbol'] = $contents[0];
		$quote['price'] = floatval($contents[1]);
		$quote['change'] = ($contents[4] > 0 ? '+' : '').floatval($contents[4]);
		$quote['open'] = floatval($contents[5]);
		$quote['high'] = floatval($contents[6]);
		$quote['low'] = floatval($contents[7]);
		$quote['date'] = $contents[2]." ".$contents[3];
		
		return $quote;
	}
	
	function acp_install()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/sql/'.$this->ipsclass->vars['sql_driver'].'_plugin_queries.php','sql_plugin_queries');
		
		if($this->ipsclass->market_class->count_plugin_filename($this->plugin['filename']) == 0)
		{
			$this->ipsclass->DB->build_query_from_cache('table_stock_companies',array(),'sql_plugin_queries');
			$this->ipsclass->DB->exec_query();
			$this->ipsclass->DB->build_query_from_cache('table_stock_shares',array(),'sql_plugin_queries');
			$this->ipsclass->DB->exec_query();
		}
	}
	
	function acp_uninstall()
	{
		if($this->ipsclass->market_class->count_plugin_filename($this->plugin['filename']) == 1)
		{
			$this->ipsclass->DB->sql_drop_table( 'market_stock_companies' );
			$this->ipsclass->DB->sql_drop_table( 'market_stock_shares' );
		}
	}
	
	function format_change($number = 0)
	{
		if($this->ipsclass->numbers->compare($number,0) >= 0)
		{
			return "<font style='color: green'>+".$this->ipsclass->numbers->format_currency($number,STOCK_DECIMAL_PLACES,1)."</font>";
		}
		else
		{
			return "<font style='color: red'>".$this->ipsclass->numbers->format_currency($number,STOCK_DECIMAL_PLACES,1)."</font>";
		}
	}
	
	function acp_menu()
	{
		return array(
					  1 => array('Stock Market','code=plugin&do=run&link='.$this->plugin['link']),
					  2 => array('Stock Companies','code=plugin&do=run&link='.$this->plugin['link'].'&action=companies'),
					  3 => array('Manage Shares','code=plugin&do=run&link='.$this->plugin['link'].'&action=search'),
					);
	}
	
	function acp_settings()
	{
		if(!$this->read_quote_yahoo_fiannce('^DJI')) // Dow Jones From Yahoo, Checking if the server can read the data
		{
			$this->ipsclass->admin->error("Unable to read from download.finance.yahoo.com. Be sure that the web server is able to read from download.finance.yahoo.com.");
		}
		
		return "";
	}
	
	function acp()
	{
		
		$redirect = array(
							'home' => 'splash_page',
							'companies' => 'list_companies',
							'add_company' => 'add_company',
							'edit_company' => 'edit_company',
							'save_company' => 'save_company',
							'delete_company' => 'delete_company',
							'search' => 'search_page',
							'list_shares' => 'list_shares',
							'edit_share' => 'edit_share',
							'save_share' => 'save_share',
							'delete_share' => 'delete_share',
		);
		
		if(!isset($redirect[$this->ipsclass->input['action']]))
		{
			$this->ipsclass->input['action'] = 'home';
		}
		
		$function = 'acp_'.$redirect[$this->ipsclass->input['action']];
		$this->$function();
	}
	
	function acp_splash_page()
	{
		$this->ipsclass->admin->page_title  = "Welcome to home page for Stock Market ACP";
		$this->ipsclass->admin->page_detail = "Welcome to home page for Stock Market ACP";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'Stock Market ACP' );
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$companies = array();
		$highestPrice = 0;
		$lowestPrice = 0;
		foreach($this->ipsclass->cache['ibm_stock_company'] as $c_id => $company)
		{
			$quote = $this->read_quote_yahoo_fiannce($company['c_symbol']);
			if(!$quote)
			{
				continue;
			}
			$company['info'] = $quote;
			$company['info']['change'] = $this->format_change($company['info']['_change'] = $this->ipsclass->numbers->multiply($company['info']['change'],$company['c_scale']));
			$company['info']['price'] = $this->ipsclass->numbers->format_currency($company['info']['_price'] = $this->ipsclass->numbers->multiply($company['info']['price'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
			$company['info']['open'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['open'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
			$company['info']['high'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['high'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
			$company['info']['low'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['low'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
			$companies[$c_id] = $company;
			
			if(!isset($companies[$highestPrice]) || $company['info']['_price'] > $companies[$highestPrice]['info']['_price'])
			{
				$highestPrice = $c_id;
			}
			if(!isset($companies[$lowestPrice]) || $company['info']['_price'] < $companies[$lowestPrice]['info']['_price'])
			{
				$lowestPrice = $c_id;
			}
		}
		$totalShares = array();
		$sumShares = 0;
		$mostShares = 0;
		$this->ipsclass->DB->build_query(array('select'=>'SUM(s_shares) as shares,s_symbol_id','from'=>'market_stock_shares','group'=>'s_symbol_id'));
		$this->ipsclass->DB->exec_query();
		while($shares = $this->ipsclass->DB->fetch_row())
		{
			$totalShares[$shares['s_symbol_id']] = $shares['shares'];
			$sumShares = $this->ipsclass->numbers->add($sumShares,$shares['shares']);
			if(!isset($totalShares[$mostShares]) || $shares['shares'] > $totalShares[$mostShares])
			{
				$mostShares = $shares['s_symbol_id'];
			}
		}
		
		$invested = 0;
		$currentValue = 0;
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_stock_shares'));
		$this->ipsclass->DB->exec_query();
		while($shares = $this->ipsclass->DB->fetch_row())
		{
			$invested = $this->ipsclass->numbers->add($this->ipsclass->numbers->multiply($shares['s_shares'],$shares['s_bought_price']),$invested);
			$currentValue = $this->ipsclass->numbers->add($this->ipsclass->numbers->multiply($shares['s_shares'],$companies[$shares['s_symbol_id']]['info']['_price']),$currentValue);
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Statistics");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																			  "Number of Companies",
																			  count($this->ipsclass->cache['ibm_stock_company'])." Companies",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Total Money Invested into Stocks",
																			  $this->ipsclass->numbers->format_currency($invested),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Stock Market Current Value",
																			  $this->ipsclass->numbers->format_currency($currentValue),
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Least Expensive share",
																			  $this->ipsclass->cache['ibm_stock_company'][$lowestPrice]['c_name']." with ".$companies[$lowestPrice]['info']['price']." per share",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Most Expensive share",
																			  $this->ipsclass->cache['ibm_stock_company'][$highestPrice]['c_name']." with ".$companies[$highestPrice]['info']['price']." per share",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																			  "Company with most shares bought",
																			  $this->ipsclass->cache['ibm_stock_company'][$mostShares]['c_name']." with ".$this->ipsclass->do_number_format($totalShares[$mostShares])." shares",
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_list_companies()
	{
		$this->ipsclass->admin->page_title  = "Stock Companies Listing";
		$this->ipsclass->admin->page_detail = "Welcome to complete listing for Stock Companies";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'Completed Auctions' );
		
		$this->ipsclass->adskin->td_header[] = array( "Symbol/Name"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Scale/Current Price" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Open Price/Change"   , "" );
		$this->ipsclass->adskin->td_header[] = array( "Low-High"            , "" );
		$this->ipsclass->adskin->td_header[] = array( "Shares/Time"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Options"             , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Stock Quotes");
		
		if(is_array($this->ipsclass->cache['ibm_stock_company']) && count($this->ipsclass->cache['ibm_stock_company']))
		{
			foreach($this->ipsclass->cache['ibm_stock_company'] as $company)
			{
				$quote = $this->read_quote_yahoo_fiannce($company['c_symbol']);
				if(!$quote)
				{
					continue;
				}
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																					  "Real Symbol (from Yahoo! Fiannance): ".$company['c_symbol']."<br />Company Name:".$company['c_name'],
																					  "Scale: ".$company['c_scale']."<br />Price: ".$this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($quote['price'],$company['c_scale']),STOCK_DECIMAL_PLACES,1),
																					  "Open Price: ".$this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($quote['open'],$company['c_scale']),STOCK_DECIMAL_PLACES,1)."<br />Change: ".$this->format_change($this->ipsclass->numbers->multiply($quote['change'],$company['c_scale'])),
																					  "Low: ".$this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($quote['low'],$company['c_scale']),STOCK_DECIMAL_PLACES,1)."<br />High: ".$this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($quote['high'],$company['c_scale']),STOCK_DECIMAL_PLACES,1),
																					  "Total Shares: ".($company['c_shares'] ? $this->ipsclass->numbers->do_number_format($company['c_shares']) : "Unlimited")."<br />Last Updated: ".$quote['date'],
																					  "<img id=\"menu_{$company['c_id']}\" src='{$this->ipsclass->vars['board_url']}/skin_acp/IPB2_Standard/images/filebrowser_action.gif' border='0' alt='Options' class='ipd' style='float:right'/>",
																			 )      );
				
				$this->ipsclass->html .= "<script type=\"text/javascript\">
	menu_build_menu( \"menu_{$company['c_id']}\",
  new Array(
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&action=edit_company&c_id={$company['c_id']}'>Edit Company</a>\",
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&action=delete_company&c_id={$company['c_id']}'>Delete Company</a>\",
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&action=view_stocks&c_id={$company['c_id']}'>View Tickets</a>\"
      )     );
 </script>";
			}
		}
		else
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No stock companies added",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->_stock_company_form(array('c_id'=>0,'c_name'=>"New Virtual Company",'c_symbol'=>"Existing stock symbol",'c_scale'=>1));
		
		$this->ipsclass->admin->output();
	}
	
	function acp_add_company()
	{
		if($this->ipsclass->input['c_id'] != 0)
		{
			$this->ipsclass->admin->error("Bad data inputed");
		}
		
		$company = $this->_process_stock_company_form();
		$this->ipsclass->DB->do_insert('market_stock_companies',$company);
		$this->company_cache();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=list","Company, ".$this->ipsclass->input['c_name'].", created...");
	}
	
	function _stock_company_form($company = array())
	{
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','prize'),
														)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->form_hidden(array(
																				array('action',$company['c_id'] ? 'save_company' : 'add_company'),
																				array('c_id',$company['c_id'])
																	)		);
		
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table($company['c_id'] ? "Edit a Stock Company" : "Add a Stock Company");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Name</b><br />The name of the company or corporation. This does not have to be the same name. It can be made up.",
																			  $this->ipsclass->adskin->form_input("c_name",$company['c_name'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Real Stock Symbol</b><br />This is the stock symbol used in the real world. It will uses the stock quote from this symbol to simulate the stock market. The stock quotes are gathered from <a href='http://finance.yahoo.com/' target='_blank'>Yahoo! Fiannace</a>. This will not be visible to the users",
																			  $this->ipsclass->adskin->form_input("c_symbol",$company['c_symbol'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Price Scaling</b><br />This will scale the pricing up or down by multiplying this number to the prices. You can use this to adjust the numbers to your economy.",
																			  $this->ipsclass->adskin->form_input("c_scale",$company['c_scale'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Avaliable Shares in existance</b><br />This is the number of shares that stays in circulation in the market. Input 0 for unlimited shares. Users will not see this number.",
																			  $this->ipsclass->adskin->form_input("c_shares",$company['c_shares'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Add/Save Company");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
	}
	
	function _process_stock_company_form()
	{
		if($this->read_quote_yahoo_fiannce($this->ipsclass->input['c_symbol']) === false)
		{
			$this->ipsclass->admin->error("This symbol does not exist in Yahoo Finannce.");
		}
		
		$company = array();
		
		$company['c_name'] = $this->ipsclass->input['c_name'];
		$company['c_symbol'] = $this->ipsclass->input['c_symbol'];
		$company['c_scale'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['c_scale'],'float');
		$company['c_scale'] = $this->ipsclass->numbers->compare($company['c_scale'],0) <= 0 ? 1 : $company['c_scale'];
		$company['c_shares'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['c_shares'],'int');
		
		return $company;
	}
	
	function acp_edit_company()
	{
		if(!is_array($this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']]))
		{
			$this->ipsclass->admin->error("Bad data inputed");
		}
		
		$this->_stock_company_form($this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']]);
		
		$this->ipsclass->admin->output();
	}
	
	function acp_save_company()
	{
		if(!is_array($this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']]))
		{
			$this->ipsclass->admin->error("Bad data inputed");
		}
		
		$company = $this->_process_stock_company_form();
		$this->ipsclass->DB->do_update('market_stock_companies',$company,"c_id=".$this->ipsclass->input['c_id']);
		$this->company_cache();
				
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=list","Company, ".$this->ipsclass->input['c_name'].", saved...");
	}
	
	function acp_delete_company()
	{
		if(!is_array($this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']]))
		{
			$this->ipsclass->admin->error("Bad data inputed");
		}
		
		$name = $this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']]['c_name'];
		$this->ipsclass->DB->do_delete('market_stock_companies',"c_id=".$this->ipsclass->input['c_id']);
		$this->ipsclass->DB->do_delete('market_stock_shares',"s_symbol_id=".$this->ipsclass->input['s_symbol_id']);
		$this->company_cache();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=list","Company, ".$name.", saved...");
	}
	
	function acp_search_page()
	{
		$companies = array();
		$companies[] = array(0,"Any Company");
		if(is_array($this->ipsclass->cache['ibm_stock_company']) && count($this->ipsclass->cache['ibm_stock_company']))
		{
			foreach($this->ipsclass->cache['ibm_stock_company'] as $company)
			{
				$quote = $this->read_quote_yahoo_fiannce($company['c_symbol']);
				if(!$quote)
				{
					continue;
				}
				$companies[] = array($company['c_id'],$company['c_name']);
			}
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to managing the stock shares";
		$this->ipsclass->admin->page_detail = "Welcome to managing the stock shares by member and/or company.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;action=search', 'Manage Shares' );
		
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','list_shares'),
											),'memberlookup');
		
		$this->ipsclass->html .= "<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:100'></div>\n";

		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Look for Stock Shares by Member Name");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Name</b><br />The name of the member to look for. Leave it blank to look at all members",
																			  $this->ipsclass->adskin->form_input("member_name","",'text',"autocomplete='off' style='width:210px' id='member_name'")
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Company</b>",
																			  $this->ipsclass->adskin->form_dropdown("company",$companies)
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Search");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= "<script type=\"text/javascript\" src='{$this->ipsclass->vars['board_url']}/jscripts/ipb_xhr_findnames.js'></script>\n";
		$this->ipsclass->html .= "<script type=\"text/javascript\">\n";
		$this->ipsclass->html .= "	init_js( 'memberlookup', 'member_name');\n";
		$this->ipsclass->html .= "	var tmp = setTimeout( 'main_loop()', 10 );\n";
		$this->ipsclass->html .= "</script>\n";
		
		$this->ipsclass->admin->output();
	}
	
	function acp_list_shares()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 50;
		
		$companies = array();
		if(is_array($this->ipsclass->cache['ibm_stock_company']) && count($this->ipsclass->cache['ibm_stock_company']))
		{
			foreach($this->ipsclass->cache['ibm_stock_company'] as $c_id => $company)
			{
				$company['info'] = $this->read_quote_yahoo_fiannce($company['c_symbol']);
				if(!$company['info'])
				{
					continue;
				}
				$company['info']['price'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['price'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
				$companies[$c_id] = $company;
			}
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to managing the stock shares";
		$this->ipsclass->admin->page_detail = "Welcome to managing the stock shares by member and/or company.";
		
		$where = array();
		if($this->ipsclass->input['member_id'])
		{
			$this->ipsclass->input['member_id'] = intval($this->ipsclass->input['member_id']);
			$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"id=".$this->ipsclass->input['member_id']));
			if(!$member)
			{
				$this->ipsclass->admin->error("Cannot find member");
			}
			$where = "s_member_id=".$member['id'];
		}
		elseif($this->ipsclass->input['member_name'])
		{
			$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
			if(!$member)
			{
				$this->ipsclass->admin->error("Cannot find member");
			}
			$this->ipsclass->input['member_id'] = $member['id'];
			$where = "s_member_id=".$member['id'];
		}
		if($this->ipsclass->input['company'])
		{
			if(!is_array($companies[$this->ipsclass->input['company']]))
			{
				$this->ipsclass->admin->error("Cannot find company");
			}
			$where = "s_symbol_id=".$this->ipsclass->input['company'];
		}
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_stock_shares','where'=>implode(" AND ",$where)));
		$page = $this->ipsclass->adskin->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'USE_ST'       => 'st',
																	'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																	'L_SINGLE'     => "Single Page",
																	'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=list_shares&amp;member_id=".$this->ipsclass->input['member_id']."&amp;company=".$this->ipsclass->input['company']
															)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "Member/Company"          , "" );
		$this->ipsclass->adskin->td_header[] = array( "Time Bought/# of Shares" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Bought/Current Price"    , "" );
		$this->ipsclass->adskin->td_header[] = array( "IP Address"              , "" );
		$this->ipsclass->adskin->td_header[] = array( "Options"                 , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Filtered Shares");
		
		$this->ipsclass->DB->build_query(array(
													'select' => 's.*',
													'from' => array('market_stock_shares' => 's'),
													'add_join' => array(
																			array(
																					'select' => 'm.members_display_name, m.mgroup',
																					'from' => array('members'=>'m'),
																					'where' => 's.s_member_id=m.id',
																					'type' => 'left'
																				),
																		),
													'where' => implode(" AND ",$where),
													'order' => "s.s_bought_time DESC",
													'limit' => array($this->ipsclass->input['st'],$LIMIT)
										)		);
		$this->ipsclass->DB->exec_query();
		while($stock = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																					$this->ipsclass->market_admin->format_name($stock['s_member_id'],$stock['members_display_name'],$stock['mgroup'])."<br />".$companies[$stock['s_symbol_id']]['c_name'],
																					"Bought Time: ".$this->ipsclass->get_date($stock['s_bought_time'],'LONG')."<br /># of shares: ".$this->ipsclass->numbers->do_number_format($stock['s_shares']),
																					"Bought Price: ".$this->ipsclass->numbers->format_currency($stock['s_bought_price'],STOCK_DECIMAL_PLACES,1)."<br />Current Price: ".$companies[$stock['s_symbol_id']]['info']['price'],
																					$stock['s_ip_address'],
																					"<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=edit_share&amp;share_id=".$stock['share_id']."&amp;member_id=".$this->ipsclass->input['member_id']."&amp;company=".$this->ipsclass->input['company']."'>Edit</a><br /><a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=delete_share&amp;share_id=".$stock['share_id']."&amp;member_id=".$this->ipsclass->input['member_id']."&amp;company=".$this->ipsclass->input['company']."'>Delete</a>",
																		 )      );
		}
		
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No shares found",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_edit_share()
	{
		$this->ipsclass->input['share_id'] = intval($this->ipsclass->input['share_id']);
		$share = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_stock_shares','where'=>"share_id=".$this->ipsclass->input['share_id']));
		if(!$share)
		{
			$this->ipsclass->admin->error("Cannot find that share");
		}
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'members_display_name','from'=>'members','where'=>"id=".$share['s_member_id']));
		
		$companies = array();
		if(is_array($this->ipsclass->cache['ibm_stock_company']) && count($this->ipsclass->cache['ibm_stock_company']))
		{
			foreach($this->ipsclass->cache['ibm_stock_company'] as $company)
			{
				$quote = $this->read_quote_yahoo_fiannce($company['c_symbol']);
				if(!$quote)
				{
					continue;
				}
				$companies[] = array($company['c_id'],$company['c_name']);
			}
		}
		
		$this->ipsclass->html .= $this->_acp_start_form(array(
																	array('action','save_share'),
																	array('share_id',$this->ipsclass->input['share_id']),
																	array('member_id',$this->ipsclass->input['member_id']),
																	array('company',$this->ipsclass->input['company']),
														)	);
		
		$this->ipsclass->html .= "<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:100'></div>\n";

		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Edit Share");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Member</b><br />The owner of these shares",
																			  $this->ipsclass->adskin->form_input("member_name",$member['members_display_name'],'text',"autocomplete='off' style='width:210px' id='member_name'")
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Company</b>",
																			  $this->ipsclass->adskin->form_dropdown("s_symbol_id",$companies,$share['s_symbol_id'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Shares</b><br />This will ignore the avaliable share limits set for the company.",
																			  $this->ipsclass->adskin->form_input("s_shares",$share['s_shares'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Bought Price</b><br />Up to 10 decimal places will be saved",
																			  $this->ipsclass->adskin->form_input("s_bought_price",$share['s_bought_price'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Bought Time</b>",
																			  $this->ipsclass->get_date($share['s_bought_time'],'LONG')
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>IP Address</b>",
																			  $share['s_ip_address']
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Save Share");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
						
		$this->ipsclass->html .= "<script type=\"text/javascript\" src='{$this->ipsclass->vars['board_url']}/jscripts/ipb_xhr_findnames.js'></script>\n";
		$this->ipsclass->html .= "<script type=\"text/javascript\">\n";
		$this->ipsclass->html .= "	init_js( 'memberlookup', 'member_name');\n";
		$this->ipsclass->html .= "	var tmp = setTimeout( 'main_loop()', 10 );\n";
		$this->ipsclass->html .= "</script>\n";
		
		$this->ipsclass->admin->output();
	}
	
	function acp_save_share()
	{
		$this->ipsclass->input['share_id'] = intval($this->ipsclass->input['share_id']);
		$share = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_stock_shares','where'=>"share_id=".$this->ipsclass->input['share_id']));
		if(!$share)
		{
			$this->ipsclass->admin->error("Cannot find that share");
		}
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		if(!$member)
		{
			$this->ipsclass->admin->error("Cannot find member");
		}
		if(!is_array($this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['s_symbol_id']]))
		{
			$this->ipsclass->admin->error("Invalid company");
		}
		$quote = $this->read_quote_yahoo_fiannce($this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['s_symbol_id']]['c_symbol']);
		if(!$quote)
		{
			$this->ipsclass->admin->error("Invalid company");
		}
		
		$this->ipsclass->DB->do_update('market_stock_shares',array(
																		's_symbol_id' => $this->ipsclass->input['s_symbol_id'],
																		's_member_id' => $member['id'],
																		's_shares' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['s_shares'],'int'),
																		's_bought_price' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['s_bought_price'],'float'),
									),"share_id=".$this->ipsclass->input['share_id']);
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=list_shares&member_id=".$this->ipsclass->input['member_id']."&company=".$this->ipsclass->input['company']."","Share Saved...");
	}
	
	function acp_delete_share()
	{
		$this->ipsclass->input['share_id'] = intval($this->ipsclass->input['share_id']);
		$this->ipsclass->DB->do_delete('market_stock_shares',"share_id=".$this->ipsclass->input['share_id']);
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=list_shares&member_id=".$this->ipsclass->input['member_id']."&company=".$this->ipsclass->input['company']."","Share Deleted...");
	}
	
	function run()
	{
		$this->ipsclass->load_template('skin_market_plugin');
		$this->ipsclass->load_language('lang_market_plugin');
		
		$this->ipsclass->market_class->title[] = array($this->plugin['name'],'market_'.$this->plugin['link']);
		if(in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['stockmarket_list_company']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=manage'>".$this->ipsclass->lang['stockmarket_manage_shares']."</a></b>",
														);
			
			$do_redirect = array(
									'list' => 'list_companies',
									'shares' => 'buy_shares_page',
									'buy' => 'buying_shares',
									'manage' => 'manage_shares',
									'resell' => 'resell_page',
									'sell' => 'resell_shares',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'list';
			}
		}
		else
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['stockmarket_list_company']."</a></b>",
														);
			
			$do_redirect = array(
									'list' => 'list_companies',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'list';
			}
		}
		
		$func = 'run_'.$do_redirect[$this->ipsclass->input['do']];
		$this->$func();
	}
	
	function run_list_companies()
	{
		$companies = array();
		
		foreach($this->ipsclass->cache['ibm_stock_company'] as $c_id => $company)
		{
			$company['info'] = $this->read_quote_yahoo_fiannce($company['c_symbol']);
			if($company['info'] === false)
			{
				continue;
			}
			
			$company['info']['change'] = $this->format_change($company['info']['_change'] = $this->ipsclass->numbers->multiply($company['info']['change'],$company['c_scale']));
			$company['info']['price'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['price'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
			$company['info']['open'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['open'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
			$company['info']['high'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['high'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
			$company['info']['low'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['low'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
			$companies[$c_id] = $company;
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->stockmarket_list_companies($companies);
	}
	
	function run_buy_shares_page()
	{
		if(!isset($this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']]))
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$company = &$this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']];
		$company['info'] = $this->read_quote_yahoo_fiannce($company['c_symbol']);
		if($company['info'] === false)
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$company['info']['change'] = $this->format_change($company['info']['_change'] = $this->ipsclass->numbers->multiply($company['info']['change'],$company['c_scale']));
		$company['info']['price'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['price'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
		$company['info']['open'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['open'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
		$company['info']['high'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['high'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
		$company['info']['low'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($company['info']['low'],$company['c_scale']),STOCK_DECIMAL_PLACES,1);
		
		if($company['c_shares'])
		{
			$data = $this->ipsclass->DB->build_and_exec(array('select'=>'SUM(s_shares) as total','from'=>'market_stock_shares','where'=>"s_symbol_id=".$this->ipsclass->input['c_id']));
			if($this->ipsclass->numbers->compare($company['c_shares'],$data['total']) < 0)
			{
				$this->ipsclass->Error(array('MSG'=>'stockmarket_no_shares_left'));
			}
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->stockmarket_buy_stock();
	}
	
	function run_buying_shares()
	{
		if(!isset($this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']]))
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$company = &$this->ipsclass->cache['ibm_stock_company'][$this->ipsclass->input['c_id']];
		$company['info'] = $this->read_quote_yahoo_fiannce($company['c_symbol']);
		if($company['info'] === false)
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$company['info']['price'] = $this->ipsclass->numbers->multiply($company['info']['price'],$company['c_scale']);
		$this->ipsclass->input['shares'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['shares'],'int');
		
		if($company['c_shares'])
		{
			$data = $this->ipsclass->DB->build_and_exec(array('select'=>'SUM(s_shares) as total','from'=>'market_stock_shares','where'=>"s_symbol_id=".$this->ipsclass->input['c_id']));
			if($this->ipsclass->numbers->compare($company['c_shares'],$data['total']) < 0)
			{
				$this->ipsclass->Error(array('MSG'=>'stockmarket_no_shares_left'));
			}
			if($this->ipsclass->numbers->compary($company['c_shares'],$this->ipsclass->numbers->add($this->ipsclass->input['shares'],$data['total'])) < 0)
			{
				$this->ipsclass->Error(array('MSG'=>'stockmarket_not_enough_left'));
			}
		}
		
		$cost = $this->ipsclass->numbers->multiply($company['info']['price'],$this->ipsclass->input['shares']);
		if($this->ipsclass->numbers->compare($cost,$this->ipsclass->member[$this->ipsclass->vars['market_members_field']]) > 0)
		{
			$this->ipsclass->Error(array('MSG'=>'stockmarket_not_enough_money'));
		}
		
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->subtract($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$cost),
				),"id=".$this->ipsclass->member['id']);
		$this->ipsclass->DB->do_insert('market_stock_shares',array(
																	's_symbol_id' => $company['c_id'],
																	's_member_id' => $this->ipsclass->member['id'],
																	's_shares' => $this->ipsclass->input['shares'],
																	's_bought_price' => $company['info']['price'],
																	's_bought_time' => time(),
																	's_ip_address' => $this->ipsclass->ip_address,
										)						);
		
		$this->ipsclass->market_class->redirect('stockmarket_bought_shares',"plugin=".$this->ipsclass->input['plugin']."&do=manage");
	}
	
	function run_manage_shares()
	{
		$shares = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_stock_shares','where'=>"s_member_id=".$this->ipsclass->member['id'],'order'=>'s_symbol_id ASC, s_bought_time ASC'));
		$q = $this->ipsclass->DB->exec_query();
		while ($share = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]))
			{
				$this->ipsclass->do_delete('market_stock_share',"share_id=".$share['share_id']);
			}
			
			if(!isset($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']))
			{
				$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info'] = $this->read_quote_yahoo_fiannce($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_symbol']);
				if($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info'] === false)
				{
					continue;
				}
				$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['change'] = $this->format_change($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['_change'] = $this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['change'],$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_scale']));
				$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['price'] = $this->ipsclass->numbers->format_currency($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['_price'] = $this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['price'],$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_scale']),STOCK_DECIMAL_PLACES,1);
				$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['open'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['open'],$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_scale']),STOCK_DECIMAL_PLACES,1);
				$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['high'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['high'],$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_scale']),STOCK_DECIMAL_PLACES,1);
				$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['low'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['low'],$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_scale']),STOCK_DECIMAL_PLACES,1);
			}
			elseif($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info'] === false)
			{
				continue;
			}
			
			$share['bought_price'] = $this->ipsclass->numbers->format_currency($share['s_bought_price'],STOCK_DECIMAL_PLACES);
			$share['old_total'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($share['s_shares'],$share['s_bought_price']));
			$share['new_total'] = $this->ipsclass->numbers->format_currency($this->ipsclass->numbers->multiply($share['s_shares'],$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['_price']));
			$share['shares'] = $this->ipsclass->do_number_format($share['s_shares']);
			
			$shares[$share['share_id']] = $share;
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->stockmarket_manage_shares($shares);
	}
	
	function run_resell_page()
	{
		$this->ipsclass->input['share'] = intval($this->ipsclass->input['share']);
		$share = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_stock_shares','where'=>"share_id=".$this->ipsclass->input['share']));
		if($share['s_member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>"incorrect_use"));
		}
		if($share['s_symbol_id'] != $this->ipsclass->input['company'])
		{
			$this->ipsclass->Error(array('MSG'=>"incorrect_use"));
		}
		if(!is_array($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]))
		{
			$this->ipsclass->Error(array('MSG'=>"incorrect_use"));
		}
		$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info'] = $this->read_quote_yahoo_fiannce($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_symbol']);
		if($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info'] === false)
		{
			$this->ipsclass->Error(array('MSG'=>"incorrect_use"));
		}
		$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['price'] = $this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['price'],$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_scale']);
		
		$share['shares'] = $this->ipsclass->do_number_format($share['s_shares']);
		$share['bought_time'] = $this->ipsclass->get_date($share['s_bought_time'],'LONG');
		$share['bought_price'] = $this->ipsclass->do_number_format($share['s_bought_price']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->stockmarket_resell_share($share);
	}
	
	function run_resell_shares()
	{
		$this->ipsclass->input['share'] = intval($this->ipsclass->input['share']);
		$share = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_stock_shares','where'=>"share_id=".$this->ipsclass->input['share']));
		if($share['s_member_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>"incorrect_use"));
		}
		if($share['s_symbol_id'] != $this->ipsclass->input['company'])
		{
			$this->ipsclass->Error(array('MSG'=>"incorrect_use"));
		}
		if(!is_array($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]))
		{
			$this->ipsclass->Error(array('MSG'=>"incorrect_use"));
		}
		$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info'] = $this->read_quote_yahoo_fiannce($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_symbol']);
		if($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info'] === false)
		{
			$this->ipsclass->Error(array('MSG'=>"incorrect_use"));
		}
		$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['price'] = $this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['price'],$this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['c_scale']);
		
		$this->ipsclass->input['shares'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['shares'],'int');
		
		if($this->ipsclass->numbers->compare($this->ipsclass->input['shares'],0) == 0)
		{
			$this->ipsclass->Error(array('MSG'=>"stockmarket_resell_at_least_one"));
		}
		if($this->ipsclass->numbers->compare($share['s_shares'],$this->ipsclass->input['shares']) < 0)
		{
			$this->ipsclass->Error(array('MSG'=>"stockmarket_dont_have_that_many"));
		}
		
		$money = $this->ipsclass->numbers->multiply($this->ipsclass->cache['ibm_stock_company'][$share['s_symbol_id']]['info']['price'],$this->ipsclass->input['shares']);
		$this->ipsclass->DB->do_update('members',array(
														$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($this->ipsclass->member[$this->ipsclass->vars['market_members_field']],$money),
				),"id=".$this->ipsclass->member['id']);
		if($this->ipsclass->numbers->compare($share['s_shares'],$this->ipsclass->input['shares']) == 0)
		{
			$this->ipsclass->DB->do_delete('market_stock_shares',"share_id=".$share['share_id']);
		}
		else
		{
			$this->ipsclass->DB->do_update('market_stock_shares',array('s_shares'=>$this->ipsclass->numbers->subtract($share['s_shares'],$this->ipsclass->input['shares'])),"share_id=".$share['share_id']);
		}
		
		$this->ipsclass->market_class->redirect('stockmarket_resold_shares',"plugin=".$this->ipsclass->input['plugin']."&do=manage");
	}
}
?>